home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 27 / CU Amiga Magazine's Super CD-ROM 27 (1998)(EMAP Images)(GB)[!][issue 1998-10].iso / CUCD / Programming / Yaroze / UploadSrc / parse.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-11-09  |  4.3 KB  |  203 lines

  1. /*
  2.  * NAME
  3.  *   parse.c - 17-May-97 18:44:07
  4.  *
  5.  * AUTHOR
  6.  *   Jon Rocatis
  7.  *
  8.  * DESCRIPTION
  9.  *   Contains the code that parses the batch files and 
  10.  *   executes the commands.
  11.  *
  12.  */
  13.  
  14. #include <ctype.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <exec/types.h>
  18. #include <devices/serial.h>
  19. #include <proto/all.h>
  20. #include <time.h>
  21. #include <string.h>
  22.  
  23. #include "upload_protos.h"
  24. #include "serial_protos.h"
  25. #include "tools_protos.h"
  26. #include "parse_protos.h"
  27. #include "myecoff.h"
  28. #include "upload.h"
  29.  
  30. /*
  31.  * NAME
  32.  *   GetKeyWords
  33.  *
  34.  * FUNCTION
  35.  *   Reads a line from the batch file and seperates the keywords
  36.  *   into ptrs.
  37.  *
  38.  */
  39.  
  40. BOOL GetKeyWords( FILE *fp, char *s1, char *s2, char *s3, char *s4 )
  41. {
  42.   char  input[255];
  43.   char *result;
  44.   
  45.     *s1 = 0;  
  46.     *s2 = 0;  
  47.     *s3 = 0;  
  48.     *s4 = 0;  
  49.   
  50.     result = fgets( input, sizeof(input), fp );
  51.     if ( result == NULL )
  52.       return( FALSE );
  53.  
  54.     sscanf( input, "%s %s %s %s\n", s1, s2, s3, s4 );
  55. //    printf( "1: %s\n2: %s\n", s1, s2 );
  56. //    printf( "3: %s\n4: %s\n\n", s3, s4 );
  57.     return( TRUE );
  58. }
  59.  
  60. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  61.  
  62. /*
  63.  * NAME
  64.  *   ParseBatchFile
  65.  *
  66.  * FUNCTION
  67.  *   Parses the batchfile line by line. Executes the recognized commands
  68.  *
  69.  * INPUT
  70.  *   fp - ptr to already opened file
  71.  *
  72.  */
  73.  
  74. BOOL LC_dload( char *str2, char *str3, char *str4 )
  75. {
  76.   long addr, fsize;
  77.   int  okay;
  78.  
  79.     if ( dontSendData )
  80.       return( TRUE );
  81.   
  82.     if ((strlen(str4) > 2) && (str4[0] == '0') && (str4[1] == 'x'))
  83.       str4 += 2;
  84.       
  85.     okay = stch_l( str4, &addr );         // Converts hex-string to binary. If (okay == 0) -> bad number!
  86.     if (okay == 0)
  87.     {
  88.       printf( "*** Illegal load address!\n");
  89.       return( FALSE ); 
  90.     }
  91.     
  92.     fsize = GetFileSize( str3 );
  93.     if ( fsize > 0 )
  94.     {
  95.       BPTR fh;
  96.       
  97.       printf( "Data load! (File:'%s', addr: $%08x - $%08x)\n", str3, addr, addr + fsize );
  98.       fh = Open( str3, MODE_OLDFILE );
  99.       if (fh)
  100.       {
  101.         DataSend( fh, addr, fsize );     // Send the whole file
  102.         WaitForPSX();
  103.         Close(fh);
  104.       }
  105.     }
  106.     else
  107.     {
  108.       printf( "*** Couldn't open '%s'!\n\n", str3 );
  109.       return( FALSE );
  110.     }
  111.     return( TRUE );
  112. }
  113.  
  114. BOOL LC_load( char *str2, char *str3, char *str4 ) 
  115. {
  116.     UploadExe( str3 );
  117.     return( TRUE );
  118. }
  119.  
  120. BOOL LC_echo( char *str2, char *str3, char *str4 ) 
  121.     printf( "%s\n", str3 ); 
  122.     return( TRUE );
  123. }
  124.  
  125. BOOL LC_sleep( char *str2, char *str3, char *str4 ) 
  126. {
  127.   int secs;
  128.   
  129.     secs = atol( str3 );
  130.     Delay( secs * 50 );
  131.     return( TRUE );
  132. }
  133.  
  134. BOOL LC_beep( char *str2, char *str3, char *str4 ) 
  135.     DisplayBeep(NULL); 
  136.     return( TRUE );
  137. }
  138.  
  139. BOOL LC_quit( char *str2, char *str3, char *str4 ) 
  140. {
  141.     return( FALSE );
  142. }
  143.  
  144. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  145.  
  146. // Table of local commands that we recognize
  147.  
  148. static struct CmdTable locCmd[] = 
  149. {
  150.   { "dload", LC_dload },
  151.   { "load",  LC_load  },
  152.   { "echo",  LC_echo  },
  153.   { "sleep", LC_sleep },
  154.   { "beep",  LC_beep  },
  155.   { "quit",  LC_quit  }
  156. };
  157.  
  158. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  159.  
  160. void ParseBatchFile( FILE *fp )
  161. {
  162.   BOOL stat;
  163.   char str1[50], str2[50], str3[50], str4[50];
  164.   
  165.     do
  166.     {
  167.       stat = GetKeyWords( fp, str1, str2, str3, str4 );
  168.       if ( stat )
  169.       {
  170.         if ((str1[0] == ';') || (str1[0] == '#'))
  171.           continue;
  172.           
  173.         if (stricmp( "local", str1 ) == 0)
  174.         {
  175.           // Command is a local command
  176.  
  177.           LONG idx;
  178.           
  179.           for ( idx = 0; idx < ArrayElements( locCmd ); idx++ )
  180.           {
  181.             if (stricmp( locCmd[idx].cmdName, str2 ) == 0)
  182.             {
  183.               if (!(*(locCmd[idx].func))( str2, str3, str4 ))     // Execute command
  184.                 stat = FALSE;
  185.               break;
  186.             }
  187.           }
  188.         }
  189.         else
  190.         {
  191.           // Command is a SIOCONS command
  192.           
  193.           if (stricmp( "go", str1 ) == 0)
  194.             SendGo();
  195.         }
  196.       }
  197.     } while (stat);
  198. }
  199.  
  200. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  201.